//
//  levelupLayer.m
//  zhuan
//
//  Created by zhao ys on 6/4/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "levelupLayer.h"
#import "playLayer.h"
#import "HelloWorldLayer.h"
#import "GameLogic.h"
#import "SoundManager.h"
#import "GameCenter.h"

@implementation levelupLayer

-(CCMenuItem *) CreateMenu:(NSString*)label target:(id)target selector:(SEL)selector {
	CCSprite * button_normal = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_unprass_01.png"]];
	CCSprite * button_select = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_onprass_01.png"]];
	CCMenuItem * button = [CCMenuItemSprite itemFromNormalSprite:button_normal selectedSprite:button_select target:target selector:selector];
	CCLabelTTF * l = [CCLabelTTF labelWithString:label fontName:@"Marker Felt" fontSize:28]; 
	l.color = ccc3(243, 189, 69);
	CGSize bSize = button.contentSize;
	l.position = ccp(bSize.width / 2, bSize.height / 2);
	[button addChild:l];
	return button;
}

// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init])) {
        [[GameLogic sharedGameLogic] addTime:-10000];
        
        [[GameLogic sharedGameLogic] save];
        
		NSLog(@"levelupLayer init\n");
		CGSize size = [[CCDirector sharedDirector] winSize];
		[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"dkz.plist"];

		CCLayer * layer = [CCLayer node];
		
		CCSprite * sprite = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_jiesuan_bg.png"]];
		[layer addChild:sprite];
		sprite.position = ccp(size.width / 2, size.height / 2);
		//score
		NSString * max_score_key = nil;
		if ([GameLogic IsScoreMode]) {
			max_score_key = @"iButtons_1";
		} else if ([GameLogic IsEndlessMode]) {
			max_score_key = @"iButtons_2";
		} else {
			max_score_key = @"iButtons_3";
		}
		NSString *path = [CCFileUtils fullPathFromRelativePath:@"game_config.plist"];
		NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];		
		int max_score = [[dict objectForKey:[NSString stringWithFormat:@"max_score_%@", max_score_key]] intValue];
		if (max_score < score_) {
			max_score = score_;
			NSString *path = [CCFileUtils fullPathFromRelativePath:@"game_config.plist"];
			NSMutableDictionary * dict =[[NSMutableDictionary alloc] initWithContentsOfFile:path];
			[dict setValue:[NSNumber  numberWithInt:max_score] forKey:[NSString stringWithFormat:@"max_score_%@", max_score_key]];
			[dict writeToFile:path atomically:YES];
			[dict release];
			if(game_center_on) {
				[[GameCenter sharedGameCenter] reportScore:score_ forCategory:max_score_key];
			}
		}
		
		CGSize sSize = sprite.contentSize;

		CCLabelTTF * label_levelup = [CCLabelTTF labelWithString:@"congratulations" fontName:@"Marker Felt"  fontSize:48];
		label_levelup.position = ccp(sSize.width / 2, sSize.height - 100);
		label_levelup.color = ccc3(64, 0, 0);
		[sprite addChild:label_levelup];
		
		[label_levelup runAction:[CCRepeatForever actionWithAction:[CCSequence actions:
								  [CCScaleTo actionWithDuration:0.9 scale:1.2], [CCScaleTo actionWithDuration:0.9 scale:0.9], nil]]];
		
		CCLabelTTF *label_score_t = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"total score  : %d", score_] fontName:@"Marker Felt" fontSize:32];
		CCLabelTTF *label_score_c = nil;
		if ([GameLogic IsClassicMode]) {
			label_score_c = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"max score    : %d", max_score] fontName:@"Marker Felt" fontSize:32];
		} else {
			label_score_c = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"current score: %d", score_cur_] fontName:@"Marker Felt" fontSize:32];
		}		CCLabelTTF *label_time    = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"time cost:     %d s", (int)time_cost_] fontName:@"Marker Felt" fontSize:32];
		label_score_t.color = label_score_c.color = label_time.color = ccc3(64, 0, 0);

		
		label_score_t.position =  ccp(sSize.width /2 , sSize.height / 2 + 60);
		label_score_c.position =  ccp(sSize.width /2 , sSize.height / 2);
		label_time.position =  ccp(sSize.width /2 , sSize.height/ 2 - 60);
		
		[sprite addChild: label_score_t];
		[sprite addChild: label_score_c];
		[sprite addChild: label_time];

		CCMenuItem * nextLevel = [self CreateMenu:@"Continue" target:self selector:@selector(OnNextLevel:)];
		CCMenuItem * mainMenu = [self CreateMenu:@"Main Menu" target:self selector:@selector(OnMainMenu:)];
		
		nextLevel.position = ccp(size.width / 2 - 120, 80);
		mainMenu.position = ccp(size.width / 2 + 120, 80);
		CCMenu * menu = [CCMenu menuWithItems:nextLevel, mainMenu, nil];
		menu.position = ccp(0,0);
		[layer addChild:menu];
		
		[self addChild:layer];
		
//		layer.position = ccp(0, -size.height);
//		[layer runAction:[CCMoveTo actionWithDuration:0.5 position:ccp(0, 0)]];
		
		sprite.position = ccp(sprite.position.x - 100, size.height + sSize.height / 2);
		sprite.rotation = -90;
		[sprite runAction:[CCMoveTo actionWithDuration:0.5 position:ccp(size.width / 2, size.height / 2)]];
		[sprite runAction:[CCRotateTo	actionWithDuration:0.5 angle:0]];
	}
	return self;
}

-(void) OnNextLevel:(id)sender {
	NSLog(@"levelupLayer OnNextLevel\n");

	if (sound_on) [[SoundManager sharedManager] play:@"enter" loops:1];

	for(int i = 0; i < 4; i++) {
		add[i] *= 0.8; //{1, 2, 3, 5}
	}
    
	if ([GameLogic IsClassicMode]) {
		[GameLogic ResetGameConfig];
	}
	
	[[GameLogic sharedGameLogic] gen];	
	
	NSLog(@"levelupLayer replaceScene playerLayer\n");

	[[CCDirector sharedDirector] replaceScene:
			[CCTransitionFade transitionWithDuration:1.0f scene:[playLayer scene]]];
}

-(void) OnMainMenu:(id)sender {
	if (sound_on) [[SoundManager sharedManager] play:@"enter" loops:1];

	[[CCDirector sharedDirector] replaceScene:
	 [CCTransitionFade transitionWithDuration:1.0f scene:[HelloWorldLayer scene]]];
}

-(void) dealloc {
	[super dealloc];
}

@end
